package br.com.bco.web.page.usuario;

import java.util.Arrays;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import br.com.bco.controle.UsuarioControl;
import br.com.bco.negocio.PerfilEnum;
import br.com.bco.negocio.Usuario;
import br.com.bco.negocio.exception.NegocioException;
import br.com.bco.web.page.AbstractPage;

/**
 * Classe Inserir Usuario
 * 
 */
public class InserirUsuario extends AbstractPage {

    @SpringBean
    private UsuarioControl usuarioControl;
    private final Usuario usuario = new Usuario();

    public InserirUsuario() {
        super();
        Form form = new Form("form", new CompoundPropertyModel(usuario));
        adicionarCampos(form);
        adicionarBotoes(form);
        add(form);
    }

    private void adicionarCampos(Form form) {
        TextField login = new TextField("login");
        PasswordTextField senha = new PasswordTextField("senha");
        TextField email = new TextField("email");
        DropDownChoice perfil =
                new DropDownChoice("perfil", Arrays.asList(PerfilEnum.values()),
                        new ChoiceRenderer("descricao"));
        form.add(login);
        form.add(senha);
        form.add(email);
        form.add(perfil);
    }

    private void adicionarBotoes(Form form) {
        Button btnGravar = new Button("btnGravar", new Model()) {
            @Override
            public void onSubmit() {
                try {
                    usuarioControl.inserir(usuario);
                } catch (NegocioException e) {
                    System.out.println(e.getMensagens().get(0));
                }
                setResponsePage(InserirUsuario.class);
            }
        };
        form.add(btnGravar);
    }

    @Override
    public String getTitulo() {
        return "Inserir Usuario";
    }
}
